package com.jie.deletefile.Weather;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.SearchView;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.jie.deletefile.R;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class CityActivity extends AppCompatActivity implements SearchView.OnQueryTextListener {

    private SearchView searchView;
    private ListView lv;
    private CityAdapter adapter ;
    List<City> city;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_city);
        initView();
        MyAsync myAsync = new MyAsync();
        myAsync.execute(URI.city);
        searchView.setOnQueryTextListener(this);

    }

    private void initView() {
        searchView = (SearchView) findViewById(R.id.search);
        lv = (ListView) findViewById(R.id.lv);
    }

    @Override
    public boolean onQueryTextSubmit(String query) {

        return false;
    }

    @Override
    public boolean onQueryTextChange(String newText) {
        Log.e("TAG", "onQueryTextChange: "+newText );
        if (adapter.getData().contains(newText)) {
            adapter.addfirst(newText.toString());
        }
        return false;
    }

    class MyAsync extends AsyncTask<String, Void, String> {



        @Override
        protected String doInBackground(String... params) {
            StringBuffer buffer = new StringBuffer();
            BufferedReader reader = null;

            try {
                URL url = new URL(params[0]);
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setConnectTimeout(3000);
                conn.setReadTimeout(3000);
                if (conn.getResponseCode() == 200) {
                    InputStream is = conn.getInputStream();
                    InputStreamReader inputStreamReader = new InputStreamReader(is);
                    reader = new BufferedReader(inputStreamReader);
                    String s = null;
                    while ((s = reader.readLine()) != null) {
                        buffer.append(s);
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
            return buffer.toString();
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            List<String> datas = new ArrayList<>();
            for (int i = 0; i < URI.data.size(); i++) {
                String s1 = URI.data.get(i).getCityName().toString();
                datas.add(s1);
            }
            adapter = new CityAdapter(CityActivity.this,R.layout.item_city,datas);
            lv.setTextFilterEnabled(true);
            lv.setAdapter(adapter);
            lv.setOnItemClickListener(itemclick);
            searchView.setSubmitButtonEnabled(true);



        }
    }
    private AdapterView.OnItemClickListener itemclick = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String city = adapter.getItem(position);
            Intent intent = new Intent(CityActivity.this, WeatherActivity.class);
            intent.putExtra("city", city);
            startActivity(intent);

        }
    };
}
